package org.spamforces.dao.impl;

import org.spamforces.dao.BroadcastDao;
import org.spamforces.model.Broadcast;

import java.util.List;

/**
 * @author IvanPopelyshev (ivan.popelyshev@gmail.com)
 */
public class BroadcastDaoImpl extends ApplicationDaoImpl<Broadcast> implements BroadcastDao {
    @Override
    public Broadcast find(long id) {
        return super.find(id);
    }

    @Override
    public List<Broadcast> findAll() {
        return findBy("TRUE ORDER BY id DESC");
    }

    @Override
    public void save(Broadcast broadcast) {
        Broadcast existingBroadcast = super.find(broadcast.getId());
        if (existingBroadcast == null) {
            broadcast.setCreationTime(super.findNow());
        }
        super.save(broadcast);
    }

    @Override
    public List<Broadcast> peek() {
        return findBy("status = 'SCHEDULED' AND scheduledTime <= NOW()");
    }
}
